// 提交表单
const submitForm = async () => {
  if (!selectedPet.value) {
    ElMessage.warning('请先选择宠物')
    return
  }

  try {
    // 表单验证
    await medicalFormRef.value.validate()

    submitLoading.value = true

    // 构建提交数据
    const submitData = {
      doctor_id: selectedPet.value.doctorId,
      reservation_id: selectedPet.value.reservationId, // 从当前选中数据获取 reservationId
      phone: selectedPet.value.phone,
      nickname: selectedPet.value.nickname,
      pet_id: selectedPet.value.petId || null, // 如果有petId的话

      diagnosis: form.value.diagnosis,
      treatment: form.value.treatment,
      hospitalization: form.value.hospitalization ? '2' : '1',
      medical_services: JSON.stringify(form.value.services),
      medications: JSON.stringify(form.value.medications.map(med => ({
        medicineId: med.medicineId,
        name: med.name,
        barCode: med.barCode,
        description: med.description
      }))),
      notes: form.value.notes
    }

    console.log('提交数据:', submitData)

    // 这里调用提交API
    await addMedication(submitData).then(resp => {
      if (resp.code === 200) {
        ElMessage.success('接诊信息提交成功！')
        resetForm()
        onMounted() // 重新加载列表
      } else {
        ElMessage.error('接诊信息提交失败！')
      }
    })

  } catch (error) {
    if (error instanceof Error) {
      ElMessage.error('表单验证失败，请检查输入')
    }
  } finally {
    submitLoading.value = false
  }
}